﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SoliderStateDead : NState<SlgEntitySoldier>
{
    public void Enter(SlgEntitySoldier entity, params object[] args)
    {
        bool deadToCamera = (bool)args[0];
        Transform camera = SlgBattleMgr.Instance.GetDeadToCamera();
        if (camera == null)
            deadToCamera = false;

        entity.m_pMoveComp.StopMove();
        entity.PlayAction(SlgFightDef.c_sAction_Idle, false);
        SlgBattleMgr.Instance.HpChange(entity.m_pVo.m_bleft, -entity.GetTroop().DeadNum2Dmg(1));
        entity.m_bDead = true;
        //DeadStrength ds = new DeadStrength(entity.m_pGameObj.transform, new Vector3(UnityEngine.Random.Range(0f, 1), 10f, UnityEngine.Random.Range(0f, 1f)), 1 * 1.5f, 10, 10, 0.2f);
        //ds.DoDead();
        SlgEntityTroop troop = entity.GetParent() as SlgEntityTroop;
        Vector3 center = troop.GetCenter();
        int x = 1;
        int z = 1;
        if (entity.m_pGameObj.transform.position.x < center.x)
            x = -1;

        if (entity.m_pGameObj.transform.position.z < center.z)
            z = -1;

        //if(camera != null)
        //{
        //    BattleEffectMgr.Instance.CreateEffectObj(11001, entity.m_pGameObj.transform.position);
        //}
        BattleEffectMgr.Instance.CreateEffectObj(11001, entity.m_pGameObj.transform.position);

        if (deadToCamera)
        {
            GameObject newPos = new GameObject();
            newPos.transform.SetParent(camera);
            Vector3 offset = camera.forward * 1.5f + camera.position + 
                            new Vector3(UnityEngine.Random.Range(-1f, 1f), UnityEngine.Random.Range(-0.75f, 0.75f), 0);
            newPos.transform.position = offset;
            entity.m_pMoveComp.UniformMoveTransform(20, newPos.transform);
            entity.m_pMoveComp.RegOnArrive(() =>
            {
                entity.m_pMoveComp.StopMove();
                entity.m_pMoveComp.ClearOnArrive();
                entity.m_pGameObj.SetActive(false);
                GameObject.Destroy(newPos);
                HeroBattleLua.C2Lua_SuiPingFX(camera.transform.localEulerAngles, 
                    SlgBattleMgr.Instance.gameObject.transform,
                    entity.m_pGameObj.transform.position);
            });
        }
        else
        {
            if (entity.m_pVo.config.soliderType != SlgFightDef.c_iArmyType_Tank)
            {
                BezierComp dead = entity.m_pGameObj.AddComponent<BezierComp>();
                dead.InitBezier(entity.m_pGameObj.transform, SlgFightDef.SDead_Path, SlgFightDef.FDead_Time,
                    entity.m_pGameObj.transform.forward, BezierComp.BezierRunType.Time, 
                    UnityEngine.Random.Range(SlgFightDef.FMinFlyHeight ,SlgFightDef.FMaxFlyHeight),() =>
                    {
                        entity.m_pGameObj.SetActive(false);
                    });
                //DeadPathStrength ds = new DeadPathStrength(entity.m_pGameObj.transform, 
                //                           new Vector3(UnityEngine.Random.Range(0f, 1f), 0f, UnityEngine.Random.Range(0f, 1f)),
                //                           1 * 1.5f, 1, 45f, 0.2f);
                //ds.DoDead();

                entity.ShadowActive(false);
            }
            else
            {
                entity.m_pMoveComp.UniformMove(10, entity.m_pGameObj.transform.position + entity.GetBackDir() * 0.25f);
            }
        }
    }

    public void Execute(SlgEntitySoldier entity, params object[] args)
    {
        
    }

    public void Exit(SlgEntitySoldier entity, params object[] args)
    {
        
    }

    public bool OnMessage(SlgEntitySoldier entity, MessageBody message)
    {
        return false;
    }
}
